package cn.itcast.haoke.dubbo.api.controller;

import cn.itcast.haoke.dubbo.api.service.PicUploadFileSystemService;
import cn.itcast.haoke.dubbo.api.vo.PicUploadResult;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@Slf4j
@RestController
@RequestMapping("pic/upload")
public class UploadController {

    //    @Resource
//    UploadImageService uploadImageService;
    @Autowired
    PicUploadFileSystemService uploadImageService;

    private static final String[] IMAGE_TYPE =
            new String[]{".bmp", ".jpg", ".jpeg", ".gif", ".png"};

    //七牛云上传请求
   /* @PostMapping
    private String upLoadImage(@RequestParam("file") MultipartFile file) throws IOException {
        //最终要返回的对象
        PicUploadResult uploadResult = new PicUploadResult();

        // 获取文件的名称
        String fileName = file.getOriginalFilename();
        boolean isLegal = false;
        for (String type : IMAGE_TYPE) {
            if (StringUtils.endsWithIgnoreCase(fileName, type)) {
                isLegal = true;
                break;
            }
        }
        if (!isLegal) {
            uploadResult.setStatus("error");
        }
        // 使用工具类根据上传文件生成唯一图片名称
        String imgName = QinuiyunUtils.getRandomImgName(fileName);

        if (!file.isEmpty()) {

            FileInputStream inputStream = (FileInputStream) file.getInputStream();

            String path = uploadImageService.uploadQNImg(inputStream, imgName);
            System.out.print("七牛云返回的图片链接:" + path);
            return path;
        }
        return "上传失败";
    }*/
    @PostMapping
    @ResponseBody
    public PicUploadResult upload(@RequestParam("file") MultipartFile uploadFile) throws Exception {
        return this.uploadImageService.upload(uploadFile);
    }
}
